package com.xdcplus.web.service.wip.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.common.pojo.dto.wip.WipUnpackRawMaterialFilterDTO;
import com.xdcplus.web.common.pojo.entity.wip.WipUnpackRawMaterial;
import com.xdcplus.web.common.pojo.query.wip.WipUnpackRawMaterialQuery;
import com.xdcplus.web.common.pojo.vo.wip.WipUnpackRawMaterialVO;
import com.xdcplus.web.mapper.wip.WipUnpackRawMaterialMapper;
import com.xdcplus.web.service.wip.WipUnpackRawMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

/**
 * 拆包箱信息表(WipUnpackRawMaterial)表服务实现类
 *
 * @author bullion.yan
 * @since 2023-05-27 15:29:44
 */
@Slf4j
@Service("wipUnpackRawMaterialService")
public class WipUnpackRawMaterialServiceImpl extends BaseServiceImpl<WipUnpackRawMaterialMapper, WipUnpackRawMaterial, WipUnpackRawMaterial, WipUnpackRawMaterialVO> implements WipUnpackRawMaterialService {

    @Autowired
    protected WipUnpackRawMaterialMapper wipUnpackRawMaterialMapper;

    @Override
    public Boolean saveOrUpdateBatch(List<WipUnpackRawMaterial> wipUnpackRawMaterialList) {

        wipUnpackRawMaterialList.forEach(wipUnpackRawMaterial -> {
            WipUnpackRawMaterial wipUnpackRawMaterialParam = new WipUnpackRawMaterial();
            wipUnpackRawMaterialParam.setId(wipUnpackRawMaterial.getId());
            if (ObjectUtil.isNotNull(wipUnpackRawMaterial.getId())) {
                wipUnpackRawMaterial.setId(wipUnpackRawMaterial.getId());
                wipUnpackRawMaterial.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<WipUnpackRawMaterial> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipUnpackRawMaterial::getId, wipUnpackRawMaterial.getId());
                update(wipUnpackRawMaterial, lambdaUpdate);
            } else {
                wipUnpackRawMaterial.setCreatedTime(new Timestamp(DateUtil.current()));
                save(wipUnpackRawMaterial);
            }
        });
        return true;
    }

    private List<WipUnpackRawMaterial> queryWipUnpackRawMaterialList(WipUnpackRawMaterialFilterDTO wipUnpackRawMaterialFilterDTO) {
        WipUnpackRawMaterialQuery wipUnpackRawMaterialQuery = BeanUtil.copyProperties(wipUnpackRawMaterialFilterDTO, WipUnpackRawMaterialQuery.class);

        return wipUnpackRawMaterialMapper.queryWipUnpackRawMaterial(wipUnpackRawMaterialQuery);
    }

    @Override
    public List<WipUnpackRawMaterialVO> queryWipUnpackRawMaterialVOList(WipUnpackRawMaterialFilterDTO wipUnpackRawMaterialFilterDTO) {
        return this.objectConversion(queryWipUnpackRawMaterialList(wipUnpackRawMaterialFilterDTO));
    }


}
